/*
Copyright (C) 2021-2025 Casa Xu (also Zhiyan Xu) from HIT

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

/* UTF-8 */



#include "LOS_SPI.h"

#define SPI_Format SPI_FMT_0

/** @fn void LOS_SPI_Init(void)
*   @brief SPI初始化函数
*/
void LOS_SPI_Init(void)
{
    //spiInit();
    mibspiInit();
}


/** @fn void LOS_SPI_Config(uint8_t CPOL, uint8_t CPHA, uint8_t wordlength)
*   @brief SPI属性配置（每次发送前调用）
*   @param[in] SPI_NUM   - SPI端口号（1、2、3、4、5），对于ASRTU，只使用1 3 5
*   @param[in] CPOL      - 时钟极性（0或1）
*   @param[in] CPHA      - 时钟相位（0或1）
*   @param[in] wordlen   - SPI传输字长，一般为8或16 
*   @note 配置SPI的时钟极性、时钟相位以及传输字长，在每次发送前调用该函数，本质是配置FMT0的值
*   考虑到软件兼容性问题，虽然TMS570支持4个FMT寄存器，但只使用FMT0，通过改变FMT0的值实现不同
*   的模式。FMT0第一次配置已在spiInit()函数中完成。
*/
void LOS_SPI_Config(uint8_t SPI_NUM, uint8_t CPOL, uint8_t CPHA, uint32_t wordlen)
{


}



/** @fn uint32_t LOS_SPI_Transmit(uint8_t SPI_NUM, uint32_t length, uint16_t * data)
*   @brief SPI发送函数（主机）
*   @param[in] SPI_NUM   - SPI端口号（1、2、3、4、5），对于ASRTU，只使用1 3 5
*   @param[in] length    - 数据长度（单位：Byte）
*   @param[in] data      - 指向待传输数据的指针
*
*   @return SPIFLG寄存器的值
*   @note 调用该函数前需调用LOS_SPI_Config函数进行配置。TMS570在上升沿取样时易丢包，经调试，发现速率
*         为500KHz（波特率预分频设置为149）时较稳定，不易丢包。
*/
uint32_t LOS_SPI_Transmit(uint8_t SPI_NUM, uint32_t length, uint16_t * data)
{
    volatile uint32_t SpiBuf;
    uint32_t errcnt = 0;

    /* SPI端口定向 */
    spiBASE_t *spiREGx = spiREG1;
    if (SPI_NUM == LOS_SPI_NUM3)
    {
        spiREGx = spiREG3;
    }
    else if (SPI_NUM == LOS_SPI_NUM5)
    {
        spiREGx = spiREG5;
    }


    uint16_t Tx_Data;
    uint32_t Chip_Select_Hold = 0x10000000U;      
    uint32_t WDelay = 0U;                //未留接口，统一不wdelay，若遇特殊情况需要wdelay，建议使用HAL库函数
    SPIDATAFMT_t DataFormat = SPI_Format; //SPI_FMT_0: CPOL=0 CPHA=0; SPI_FMT_1: CPOL=0  CPHA=0; SPI_FMT_2: CPOL=0  CPHA=0; SPI_FMT_3: CPOL=1 CPHA=0;
    uint8_t ChipSelect = 0;        //片选通过直接操作引脚来实现，不需要该位

    /* SPI发送 */
    while(length != 0U)
    {
        if((spiREGx->FLG & 0x000000FFU) !=0U)
        {
           break;
        }
      
        Tx_Data = *data;

        spiREGx->DAT1 =  ((uint32)DataFormat << 24U) |
                         ((uint32)ChipSelect << 16U) |
                         (WDelay)           |
                         (Chip_Select_Hold) |
                         (uint32)Tx_Data;
    
        data++;

        while((spiREGx->FLG & 0x00000100U) != 0x00000100U)  //检测是否接收到从机数据，以此判断发送是否完成
        {
            errcnt++;
            if (errcnt>1000)
            {
                LOS_SPI_ERR(LOS_SPI_ERR_TRAN);
                break;
            }
        } /* Wait */
        SpiBuf = spiREGx->BUF; //忽略接收到的数据，数据在函数返回后自动丢失

        length--;
        
    }

    return (spiREGx->FLG & 0xFFU);
}


/** @fn uint32_t LOS_SPI_Receive(uint8_t SPI_NUM, uint32_t length, uint16_t * data)
*   @brief SPI接收函数（主机）
*   @param[in] SPI_NUM   - SPI端口号（1、2、3、4、5），对于ASRTU，只使用1 3 5
*   @param[in] length    - 数据长度（单位：Byte）
*   @param[in] data      - 指向存放接收数据的指针
*   @param[in] CS_MASK   - 片选掩码，需要使用的片选口置0，不使用的置1（亲测如此，官方文档写反了）
*
*   @return SPIFLG寄存器的值
*   @note 调用该函数前需调用LOS_SPI_Config函数进行配置。TMS570在上升沿取样时易丢包，经调试，发现速率
*         为500KHz（波特率预分频设置为149）时较稳定，不易丢包。
*/
uint32_t LOS_SPI_Receive(uint8_t SPI_NUM, uint32_t length, uint16_t * data)
{
    uint32_t errcnt = 0;

    /* SPI端口定向 */
    spiBASE_t *spiREGx = spiREG1;
    if (SPI_NUM == LOS_SPI_NUM3)
    {
        spiREGx = spiREG3;
    }
    else if (SPI_NUM == LOS_SPI_NUM5)
    {
        spiREGx = spiREG5;
    }


    uint32_t Chip_Select_Hold = 0x10000000U;      
    uint32_t WDelay = 0U;                //未留接口，统一不wdelay，若遇特殊情况需要wdelay，建议使用HAL库函数
    SPIDATAFMT_t DataFormat = SPI_Format;
    uint8_t ChipSelect = 0;        //片选掩码，0为使能，1为禁用

    /* SPI发送 */
    while(length != 0U)
    {
        if((spiREGx->FLG & 0x000000FFU) !=0U)
        {
           break;
        }

        spiREGx->DAT1 =  ((uint32)DataFormat << 24U) |
                     ((uint32)ChipSelect << 16U) |
                     (WDelay)           |
                     (Chip_Select_Hold) |
                     (0x0000FFFFU);
    


        while((spiREGx->FLG & 0x00000100U) != 0x00000100U)  //检测是否接收到从机数据，以此判断发送是否完成
        {
            errcnt++;
            if (errcnt>1000)
            {
                LOS_SPI_ERR(LOS_SPI_ERR_RECV);
                break;
            }
        } 

        *data = (uint16_t)spiREGx->BUF; //拷贝接收到的数据

        data++;
        length--;
    }

    return (spiREGx->FLG & 0xFFU);
}


/** @fn uint32_t LOS_SPI_TR(uint8_t SPI_NUM, uint32_t length, uint16_t * data_t, uint16_t * data_r, uint8_t CS_MASK)
*   @brief SPI发送接收函数（主机）
*   @param[in] SPI_NUM   - SPI端口号（1、2、3、4、5），对于ASRTU，只使用1 3 5
*   @param[in] length    - 数据长度（单位：Byte）
*   @param[in] data_t    - 指向待传输数据的指针
*   @param[in] data_r    - 指向数据接收区的指针
*   @param[in] CS_MASK   - 片选掩码，需要使用的片选口置0，不使用的置1（亲测如此，官方文档写反了）
*
*   @return SPIFLG寄存器的值
*   @note 调用该函数前需调用LOS_SPI_Config函数进行配置。TMS570在上升沿取样时易丢包，经调试，发现速率
*         为500KHz（波特率预分频设置为149）时较稳定，不易丢包。
*/
uint32_t LOS_SPI_TR(uint8_t SPI_NUM, uint32_t length, uint16_t * data_t, uint16_t * data_r)
{
    uint32_t errcnt = 0;

    /* SPI端口定向 */
    spiBASE_t *spiREGx = spiREG1;
    if (SPI_NUM == LOS_SPI_NUM3)
    {
        spiREGx = spiREG3;
    }
    else if (SPI_NUM == LOS_SPI_NUM5)
    {
        spiREGx = spiREG5;
    }


    uint16_t Tx_Data;
    uint32_t Chip_Select_Hold = 0;      
    uint32_t WDelay = 0U;                //未留接口，统一不wdelay，若遇特殊情况需要wdelay，建议使用HAL库函数
    SPIDATAFMT_t DataFormat = SPI_Format; //
    uint8_t ChipSelect = 0;        //片选掩码，0为使能，1为禁用

    /* SPI发送 */
    while(length != 0U)
    {
        if((spiREGx->FLG & 0x000000FFU) !=0U)
        {
           break;
        }
      
        Tx_Data = *data_t;

        spiREGx->DAT1 =  ((uint32)DataFormat << 24U) |
                     ((uint32)ChipSelect << 16U) |
                     (WDelay)           |
                     (Chip_Select_Hold) |
                     (uint32)Tx_Data;
    
        data_t++;

        while((spiREGx->FLG & 0x00000100U) != 0x00000100U)  //检测是否接收到从机数据，以此判断发送是否完成
        {
            errcnt++;
            if (errcnt>1000)
            {
                LOS_SPI_ERR(LOS_SPI_ERR_TRAN);
                break;
            }
        } /* Wait */
        *data_r = spiREGx->BUF; //转存接收到的数据
        
        data_r++;

        length--;
    }

    return (spiREGx->FLG & 0xFFU);
}


/** @fn void LOS_SPI_ERR(uint8_t ERR_SIT)
*   @brief SPI错误处理函数
*/
void LOS_SPI_ERR(uint8_t ERR_SIT)
{

}


/** @fn uint16_t LOS_SPI_WriteReadByte(uint8_t SPI_NUM, uint16_t Byte, uint16_t CS_NUM)
*   @brief SPI读写一个字节
*   @param[in] SPI_NUM   - SPI端口号（1、2、3、4、5），对于ASRTU，只使用1 3 5
*   @param[in] Byte      - 发送字节
*   @param[in] CS_NUM    - 片选号，格式为CS_0, CS_1, CS_2, CS_3等
*   @return RXByte - MISO接收到的字节
*   @note 该函数运行完成后，片选不会拉高，在发送最后一个字节之前的字节时使用
*/
uint16_t LOS_SPI_WriteReadByte(uint8_t SPI_NUM, uint16_t Byte, uint16_t CS_NUM)
{
    uint16_t TXByte = Byte;
    uint16_t RXByte = 0x44;
    uint32_t errcnt = 0;

    LOS_SPI_Start(SPI_NUM);

    if (SPI_NUM == LOS_SPI_NUM1)
    {
        mibspiRAM1->tx[0].control = (uint16)((uint16)4U << 13U)  /* buffer mode */
                                  | (uint16)((uint16)1U << 12U) /* chip select hold */
                                  | (uint16)((uint16)0U << 10U)  /* enable WDELAY */
                                  | (uint16)((uint16)0U << 8U)  /* data format */
                                  | ((uint16)(~((uint16)0xFFU ^ (uint16)CS_NUM)) & (uint16)0x00FFU);  /* chip select */

        mibspiSetData(mibspiREG1, 0, &TXByte);
        mibspiTransfer(mibspiREG1, 0);

        while(mibspiIsTransferComplete(mibspiREG1,0)!=TRUE)//等待发送完成
        {
            errcnt++;
            if (errcnt>500000)
            {
                break;
            }
        }
        errcnt = 0;
    
        mibspiGetData(mibspiREG1,0,&RXByte);
    } 
    else if (SPI_NUM == LOS_SPI_NUM3)
    {
        mibspiRAM3->tx[0].control = (uint16)((uint16)4U << 13U)  /* buffer mode */
                                  | (uint16)((uint16)1U << 12U) /* chip select hold */
                                  | (uint16)((uint16)0U << 10U)  /* enable WDELAY */
                                  | (uint16)((uint16)1U << 8U)  /* data format */
                                  | ((uint16)(~((uint16)0xFFU ^ (uint16)CS_NUM)) & (uint16)0x00FFU);  /* chip select */

        mibspiSetData(mibspiREG3, 0, &TXByte);
        mibspiTransfer(mibspiREG3, 0);

        while(mibspiIsTransferComplete(mibspiREG3,0)!=TRUE)//等待发送完成
        {
            errcnt++;
            if (errcnt>500000)
            {
                break;
            }
        }
        errcnt = 0;
    
        mibspiGetData(mibspiREG3,0,&RXByte);
    }
    else if (SPI_NUM == LOS_SPI_NUM5)
    {
        if (CS_NUM == CS_2) //MEMS陀螺
        {
            mibspiRAM5->tx[6].control = (uint16)((uint16)4U << 13U)  /* buffer mode */
                                    | (uint16)((uint16)1U << 12U) /* chip select hold */
                                    | (uint16)((uint16)0U << 10U)  /* enable WDELAY */
                                    | (uint16)((uint16)2U << 8U)  /* data format */
                                    | ((uint16)(~((uint16)0xFFU ^ (uint16)CS_NUM)) & (uint16)0x00FFU);  /* chip select */
            mibspiSetData(mibspiREG5, 6, &TXByte);
            mibspiTransfer(mibspiREG5, 6);

            while(mibspiIsTransferComplete(mibspiREG5,6)!=TRUE)
            {
                errcnt++;
                if (errcnt>500000)
                {
                    break;
                }
            }
            errcnt = 0;
    
            mibspiGetData(mibspiREG5,6,&RXByte);
        }
        else
        {
            mibspiRAM5->tx[0].control = (uint16)((uint16)4U << 13U)  /* buffer mode */
                                    | (uint16)((uint16)1U << 12U) /* chip select hold */
                                    | (uint16)((uint16)0U << 10U)  /* enable WDELAY */
                                    | (uint16)((uint16)1U << 8U)  /* data format */
                                    | ((uint16)(~((uint16)0xFFU ^ (uint16)CS_NUM)) & (uint16)0x00FFU);  /* chip select */

            mibspiSetData(mibspiREG5, 0, &TXByte);
            mibspiTransfer(mibspiREG5, 0);

            while(mibspiIsTransferComplete(mibspiREG5,0)!=TRUE)
            {
                errcnt++;
                if (errcnt>500000)
                {
                    break;
                }
            }
            errcnt = 0;
    
            mibspiGetData(mibspiREG5,0,&RXByte);
        }
    }

    return RXByte;
}


/** @fn uint16_t LOS_SPI_WriteReadLastByte(uint8_t  SPI_NUM, uint16_t Byte, uint16_t CS_NUM)
*   @brief SPI读写最后一个字节
*   @param[in] SPI_NUM   - SPI端口号（1、2、3、4、5），对于ASRTU，只使用1 3 5
*   @param[in] Byte      - 发送字节
*   @param[in] CS_NUM    - 片选号，格式为CS_0, CS_1, CS_2, CS_3等
*   @return RXByte - MISO接收到的字节
*   @note 该函数运行完成后，片选会拉高，必须使用该函数读写最后一个字节
*/
uint16_t LOS_SPI_WriteReadLastByte(uint8_t  SPI_NUM, uint16_t Byte, uint16_t CS_NUM)
{
    uint16_t TXByte = Byte;
    uint16_t RXByte = 0x44;
    uint32_t i = 0;
    uint32_t errcnt = 0;

    LOS_SPI_Start(SPI_NUM);
    if (SPI_NUM == LOS_SPI_NUM1)
    {
        mibspiRAM1->tx[1].control = (uint16)((uint16)4U << 13U)  /* buffer mode */
                                | (uint16)((uint16)0U << 12U) /* chip select hold */
                                | (uint16)((uint16)0U << 10U)  /* enable WDELAY */
                                | (uint16)((uint16)0U << 8U)  /* data format */
                                | ((uint16)(~((uint16)0xFFU ^ (uint16)CS_NUM)) & (uint16)0x00FFU);  /* chip select */

        mibspiSetData(mibspiREG1, 1, &TXByte);
        mibspiTransfer(mibspiREG1, 1);

        while(mibspiIsTransferComplete(mibspiREG1, 1) != TRUE)//等待发送完成
        {
            errcnt++;
            if (errcnt>500000)
            {
                break;
            }
        }
        errcnt = 0;
        //for(i=0;i<1000;i++);
        mibspiGetData(mibspiREG1, 1, &RXByte);
    }
    else if (SPI_NUM == LOS_SPI_NUM3)
    {
        mibspiRAM3->tx[1].control = (uint16)((uint16)4U << 13U)  /* buffer mode */
                                | (uint16)((uint16)0U << 12U) /* chip select hold */
                                | (uint16)((uint16)0U << 10U)  /* enable WDELAY */
                                | (uint16)((uint16)1U << 8U)  /* data format */
                                | ((uint16)(~((uint16)0xFFU ^ (uint16)CS_NUM)) & (uint16)0x00FFU);  /* chip select */

        mibspiSetData(mibspiREG3, 1, &TXByte);
        mibspiTransfer(mibspiREG3, 1);

        while(mibspiIsTransferComplete(mibspiREG3, 1) != TRUE)//等待发送完成
        {
            errcnt++;
            if (errcnt>500000)
            {
                break;
            }
        }
        errcnt = 0;

        mibspiGetData(mibspiREG3, 1, &RXByte);
    }
    else if (SPI_NUM == LOS_SPI_NUM5)
    {
        if (CS_NUM == CS_2)
        {
            mibspiRAM5->tx[1].control = (uint16)((uint16)4U << 13U)  /* buffer mode */
                                    | (uint16)((uint16)0U << 12U) /* chip select hold */
                                    | (uint16)((uint16)0U << 10U)  /* enable WDELAY */
                                    | (uint16)((uint16)2U << 8U)  /* data format */
                                    | ((uint16)(~((uint16)0xFFU ^ (uint16)CS_NUM)) & (uint16)0x00FFU);  /* chip select */

            mibspiSetData(mibspiREG5, 1, &TXByte);
            mibspiTransfer(mibspiREG5, 1);

            while(mibspiIsTransferComplete(mibspiREG5, 1) != TRUE)//等待发送完成
            {
                errcnt++;
                if (errcnt>500000)
                {
                    break;
                }
            }
            errcnt = 0;

            mibspiGetData(mibspiREG5, 1, &RXByte);
        }
        else
        {
           mibspiRAM5->tx[1].control = (uint16)((uint16)4U << 13U)  /* buffer mode */
                                   | (uint16)((uint16)0U << 12U) /* chip select hold */
                                   | (uint16)((uint16)0U << 10U)  /* enable WDELAY */
                                   | (uint16)((uint16)1U << 8U)  /* data format */
                                   | ((uint16)(~((uint16)0xFFU ^ (uint16)CS_NUM)) & (uint16)0x00FFU);  /* chip select */

           mibspiSetData(mibspiREG5, 1, &TXByte);
           mibspiTransfer(mibspiREG5, 1);

           while(mibspiIsTransferComplete(mibspiREG5, 1) != TRUE)//等待发送完成
           {
               errcnt++;
               if (errcnt>500000)
               {
                   break;
               }
           }
           errcnt = 0;

           mibspiGetData(mibspiREG5, 1, &RXByte);
        }
    }
    LOS_SPI_Stop(SPI_NUM);
    return RXByte;
    
}

void LOS_SPI_Start(uint8_t SPI_NUM)
{
    
    if (SPI_NUM == LOS_SPI_NUM3)
    {
        mibspiREG3->PC0 = (uint32)((uint32)1U << 0U)  /* SCS[0] */
                        | (uint32)((uint32)1U << 1U)  /* SCS[1] */
                        | (uint32)((uint32)0U << 2U)  /* SCS[2] */
                        | (uint32)((uint32)0U << 3U)  /* SCS[3] */
                        | (uint32)((uint32)0U << 4U)  /* SCS[4] */
                        | (uint32)((uint32)1U << 5U)  /* SCS[5] */
                        | (uint32)((uint32)0U << 8U)  /* ENA */
                        | (uint32)((uint32)1U << 9U)  /* CLK */
                        | (uint32)((uint32)1U << 10U)  /* SIMO */
                        | (uint32)((uint32)1U << 11U); /* SOMI */
    }
    else if (SPI_NUM == LOS_SPI_NUM5)
    {
        mibspiREG5->PC0 = (uint32)((uint32)1U << 0U)  /* SCS[0] */
                        | (uint32)((uint32)1U << 1U)  /* SCS[1] */
                        | (uint32)((uint32)1U << 2U)  /* SCS[2] */
                        | (uint32)((uint32)0U << 3U)  /* SCS[3] */
                        | (uint32)((uint32)0U << 8U)  /* ENA */
                        | (uint32)((uint32)1U << 9U)  /* CLK */
                        | (uint32)((uint32)1U << 10U)  /* SIMO[0] */
                        | (uint32)((uint32)1U << 11U)  /* SOMI[0] */
                        | (uint32)((uint32)0U << 17U)  /* SIMO[1] */
                        | (uint32)((uint32)0U << 18U)  /* SIMO[2] */
                        | (uint32)((uint32)0U << 19U)  /* SIMO[3] */
                        | (uint32)((uint32)0U << 25U)  /* SOMI[1] */
                        | (uint32)((uint32)0U << 26U)  /* SOMI[2] */
                        | (uint32)((uint32)0U << 27U); /* SOMI[3] */
    }
    else if (SPI_NUM == LOS_SPI_NUM1)
    {
        mibspiREG1->PC0 = (uint32)((uint32)1U << 0U)  /* SCS[0] */
                        | (uint32)((uint32)1U << 1U)  /* SCS[1] */
                        | (uint32)((uint32)1U << 2U)  /* SCS[2] */
                        | (uint32)((uint32)1U << 3U)  /* SCS[3] */
                        | (uint32)((uint32)1U << 4U)  /* SCS[4] */
                        | (uint32)((uint32)0U << 5U)  /* SCS[5] */
                        | (uint32)((uint32)0U << 8U)  /* ENA */
                        | (uint32)((uint32)1U << 9U)  /* CLK */
                        | (uint32)((uint32)1U << 10U)  /* SIMO[0] */
                        | (uint32)((uint32)1U << 11U)  /* SOMI[0] */
                        | (uint32)((uint32)0U << 17U)  /* SIMO[1] */
                        | (uint32)((uint32)0U << 25U); /* SOMI[1] */ 
    }
    

}

void LOS_SPI_Stop(uint8_t SPI_NUM)
{
    
    if (SPI_NUM == LOS_SPI_NUM3)
    {
        mibspiREG3->PC0 = (uint32)((uint32)1U << 0U)  /* SCS[0] */
                        | (uint32)((uint32)1U << 1U)  /* SCS[1] */
                        | (uint32)((uint32)0U << 2U)  /* SCS[2] */
                        | (uint32)((uint32)0U << 3U)  /* SCS[3] */
                        | (uint32)((uint32)0U << 4U)  /* SCS[4] */
                        | (uint32)((uint32)1U << 5U)  /* SCS[5] */
                        | (uint32)((uint32)0U << 8U)  /* ENA */
                        | (uint32)((uint32)1U << 9U)  /* CLK */
                        | (uint32)((uint32)0U << 10U)  /* SIMO */
                        | (uint32)((uint32)0U << 11U); /* SOMI */
    }
    else if (SPI_NUM == LOS_SPI_NUM5)
    {
        mibspiREG5->PC0 = (uint32)((uint32)1U << 0U)  /* SCS[0] */
                        | (uint32)((uint32)1U << 1U)  /* SCS[1] */
                        | (uint32)((uint32)1U << 2U)  /* SCS[2] */
                        | (uint32)((uint32)0U << 3U)  /* SCS[3] */
                        | (uint32)((uint32)0U << 8U)  /* ENA */
                        | (uint32)((uint32)1U << 9U)  /* CLK */
                        | (uint32)((uint32)0U << 10U)  /* SIMO[0] */
                        | (uint32)((uint32)0U << 11U)  /* SOMI[0] */
                        | (uint32)((uint32)0U << 17U)  /* SIMO[1] */
                        | (uint32)((uint32)0U << 18U)  /* SIMO[2] */
                        | (uint32)((uint32)0U << 19U)  /* SIMO[3] */
                        | (uint32)((uint32)0U << 25U)  /* SOMI[1] */
                        | (uint32)((uint32)0U << 26U)  /* SOMI[2] */
                        | (uint32)((uint32)0U << 27U); /* SOMI[3] */  
    }
    else if (SPI_NUM == LOS_SPI_NUM1)
    {
        mibspiREG1->PC0 = (uint32)((uint32)1U << 0U)  /* SCS[0] */
                        | (uint32)((uint32)1U << 1U)  /* SCS[1] */
                        | (uint32)((uint32)1U << 2U)  /* SCS[2] */
                        | (uint32)((uint32)1U << 3U)  /* SCS[3] */
                        | (uint32)((uint32)1U << 4U)  /* SCS[4] */
                        | (uint32)((uint32)0U << 5U)  /* SCS[5] */
                        | (uint32)((uint32)0U << 8U)  /* ENA */
                        | (uint32)((uint32)1U << 9U)  /* CLK */
                        | (uint32)((uint32)0U << 10U)  /* SIMO[0] */
                        | (uint32)((uint32)0U << 11U)  /* SOMI[0] */
                        | (uint32)((uint32)0U << 17U)  /* SIMO[1] */
                        | (uint32)((uint32)0U << 25U); /* SOMI[1] */ 
    }

}




uint16_t LOS_SPI_MEMS(uint16_t Bytes)
{
    return LOS_SPI_WriteReadLastByte(LOS_SPI_NUM5, Bytes, CS_2);
}
